﻿@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}
<div class="row">
    <div class="col-md-3">
        <input class="btn btn-primary" type="button" id="adminlogin" value="admin登录" />
    </div>
    <div class="col-md-3">
        <input class="btn btn-primary" type="button" id="systemlogin" value="system登录" />
    </div>
    <div class="col-md-3">
        <input class="btn btn-primary" type="button" id="errorlogin" value="error username登录" />
    </div>
    <div class="col-md-3">
        <input class="btn btn-primary" type="button" id="getvalues" value="获取Values" />
    </div>
</div>
@section Scripts{
    <script>
        var token;
        $(function () {
            $("#adminlogin").click(function () {

                $.ajax({
                    url: "http://localhost:39287/api/login",
                    dataType: 'json',
                    type: 'post',
                    xhrFields: {
                        withCredentials: true
                    },
                    data: { username: "gsw", password: "111111", role: "admin" },
                    success: function (result) {
                        console.log(result);
                        token = result.access_token;
                    }
                });
            })
        })
        $(function () {
            $("#systemlogin").click(function () {
                $.ajax({
                    url: "http://localhost:39287/api/login",
                    dataType: 'json',
                    type: 'post',
                    xhrFields: {
                        withCredentials: true
                    },
                    data: { username: "gsw", password: "111111", role: "system" },
                    success: function (result) {
                        console.log(result);
                        token = result.access_token;
                    }
                });
            })
        })
        $(function () {
            $("#errorlogin").click(function () {
                $.ajax({
                    url: "http://localhost:39287/api/login",
                    dataType: 'json',
                    type: 'post',
                    xhrFields: {
                        withCredentials: true
                    },
                    data: { username: "gswaa", password: "111111", role: "admin" },
                    success: function (result) {
                        console.log(result);
                        token = result.access_token;
                    }
                });
            })
        })

        $("#getvalues").click(function () {
            $.ajax({
                url: "http://localhost:39287/api/values",
                dataType: 'json',
                type: 'get',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Bearer " + token);
                },
                xhrFields: {
                    withCredentials: true
                },            
                cache: false,
                success: function (result) {
                    console.log(result);
                }
            });
        })


    </script>
}
